import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as dotenv from 'dotenv';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
import { json } from 'express';

dotenv.config();

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 配置CORS
  app.enableCors({
    origin: true,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
    credentials: true,
  });

  // 修改静态文件服务配置
  const publicPath = join(process.cwd(), 'public');
  console.log('静态文件目录:', publicPath);

  app.useStaticAssets(publicPath, {
    index: 'index.html',
    redirect: true,
  });

  app.use(json({ limit: '50mb' }));

  await app.listen(3000);
  console.log('服务器已启动在: http://localhost:3000');
}

bootstrap()
  .then(() => {
    console.log('应用已成功启动');
  })
  .catch((error) => {
    console.error('启动应用时发生错误:', error);
  });
